<template>
  <view>
    <view class="my-search-container">
      <!-- 使用 view 组件模拟 input 输入框的样式 -->
      <uni-search-bar @input="input" :radius="100" cancelButton="none" v-model="kw"></uni-search-bar>

    </view>
    <!-- 搜索列表-->
    <view class="kw-list" v-if="kwGoodsList.length">
      <view class="looklist" @click="goToGoodsList()">
        查看详情
      </view>
      <view class="kw-item" v-for="(item,i) in kwGoodsList" :key="i" @click="gotoDetail(item.goods_id)">
        <!-- 内容-->
        <view class="kw-title">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史-->
    <view class="history" v-else>
      <view class="history-title">
        <view>搜索历史</view>
        <uni-icons type="trash" @click="deleteKw()"></uni-icons>
      </view>
      <view class="history-box">
        <uni-tag circle class="history-item" v-for="(item,i) in rKwList" :key="i" :text="item" @click="toSearch(item)">
        </uni-tag>
      </view>
    </view>
  </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null, //延时器
        kw: '', //搜索关键字
        kwGoodsList: [], //关键字获取商品数组
        //关键字数组
        kwList: JSON.parse(uni.getStorageSync('kw') || '[]')
      };
    },
    //改变触发
    computed: {
      rKwList() {
        //数组翻转
        return [...this.kwList].reverse()
      }
    },

    methods: {
      input(a) {
        //清空延时器
        clearTimeout(this.timer)
        // 重新启动延时器,1秒内不重新触发输入事件,则进行下面的搜索功能
        this.timer = setTimeout(() => {
          this.kw = a
          //发送请求获取相关数据
          this.getKwGoodsList()
        }, 1000)
      },
      //跳转到商品列表页面
      goToGoodsList() {
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?query=${this.kw}`
        })
      },
      async getKwGoodsList() {
        //判断是否有关键字
        if (!this.kw) {
          //清空列表数据
          this.kwGoodsList = []
          //停止下面操作
          return
        }
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        this.kwGoodsList = res.message
        this.savekwList()

      },
      //获取每次搜索的关键字
      savekwList() {
        const set = new Set(this.kwList)
        //先删除已经有的
        set.delete(this.kw)
        //在添加
        set.add(this.kw)
        //将set对象转换成数组
        this.kwList = Array.from(set)
        uni.setStorageSync('kw', JSON.stringify(this.kwList))
      },
      gotoDetail(id) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
        })
      },
      //删除历史关键字
      deleteKw() {
        this.kwList = []
        uni.setStorageSync('kw', '[]')
      },
      //点击关键字进行搜索

      toSearch(item) {
        this.kw = item
        this.getKwGoodsList()
      }
    }
  }
</script>

<style lang="scss">
  .my-search-container {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  .kw-list {
    padding: 0 20rpx;

    .kw-item {
      display: flex;
      font-size: 24rpx;
      padding: 20rpx 0;
      border-bottom: 1rpx solid #C0C0C0;
      align-items: center;
      justify-content: space-between;

      .kw-title {
        white-space: nowrap; //不换行
        overflow: hidden; //溢出隐藏
        text-overflow: ellipsis; //溢出文字用...
        margin-right: 10rpx;
      }
    }
  }

  .history {
    padding: 0 5rpx;
    height: 80rpx;
    color: #808080;
    font-size: 26rpx;
    line-height: 80rpx;

    .history-title {
      margin: 0rpx 40rpx;
      display: flex;
      justify-content: space-between;
    }

    .history-box {
      border-top: 1rpx solid #ccc;
      display: flex;

      .history-item {
        margin: 10rpx 4rpx;
        padding: 4rpx 10rpx;
      }
    }
  }
  .looklist{
    height: 50rpx;
    background-color: #f00;
    border-top: 2rpx solid #fff;
    border-radius: 30rpx;
    color: #FFFFFF;
    font-weight: 700;
    text-align: center;
    line-height: 50rpx;
  }
</style>
